﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Aandelendelen.Caching;
using System.Web.Caching;
using System.Xml;

namespace Aandelendelen.Obs
{
	public class Maintenance : ICacheable
	{
		public static string CacheName = "data_maintenance"; 

		private string result = "";

		public Maintenance()
		{
			InitCacheKey();

			XmlDocument doc = new XmlDocument();

			doc.Load(HttpContext.Current.Server.MapPath("/App_Data/Maintenance.xml"));

			XmlNode node = doc.DocumentElement.ChildNodes[0];

			int endHour = Convert.ToInt32(node.Attributes["endhour"].Value);
			int endMinute = Convert.ToInt32(node.Attributes["endminute"].Value);

			if (endHour == 0 && endMinute == 0)
				result = string.Empty;
			else
			{
				DateTime end = new DateTime(1, 1, 1, endHour, endMinute, 0);

				result = end.ToString("HH:mm");
			}
		}

		public override string ToString()
		{
			return result;
		}

		public static Maintenance GetInstance()
		{
			return DataCache.GetCacheableObject<Maintenance>(CacheName);
		}

		public static Maintenance GetInstance(string cacheKey)
		{
			return DataCache.GetCacheableObject<Maintenance>(cacheKey);
		}

#region ICacheable 
		public CacheKey CacheKey { get; set; }

		public void CacheThis(string cacheKey)
		{
			this.CacheKey.CacheThis(cacheKey);
		}

		public void InitCacheKey()
		{
			CacheKey = new CacheKey()
			{
				CacheDependency = new CacheDependency(HttpContext.Current.Server.MapPath("/App_Data/Maintenance.xml")),
				ObjectToCache = this
			};
		}
#endregion
	}
}